From a12a909c04e6ce8d48e8db016624ac4770f3101d Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Sat, 12 Jul 2014 15:30:24 -0700 Subject: [PATCH] Pass --cfg ndebug when debug = false This will disable debug!() and debug_assert!() statements. --- src/cargo/ops/cargo_rustc/mod.rs | 5 +++++ tests/test_cargo_compile.rs | 38 ++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/src/cargo/ops/cargo_rustc/mod.rs b/src/cargo/ops/cargo_rustc/mod.rs index e2b115a14..779444258 100644 --- a/src/cargo/ops/cargo_rustc/mod.rs +++ b/src/cargo/ops/cargo_rustc/mod.rs @@ -255,6 +255,11 @@ fn build_base_args(into: &mut Args, // into.push("-g".to_string()); // } + if !profile.get_debug() { + into.push("--cfg".to_string()); + into.push("ndebug".to_string()); + } + if profile.is_test() { into.push("--test".to_string()); } diff --git a/tests/test_cargo_compile.rs b/tests/test_cargo_compile.rs index a4a5d62c2..fa539e5e9 100644 --- a/tests/test_cargo_compile.rs +++ b/tests/test_cargo_compile.rs @@ -1030,6 +1030,7 @@ test!(verbose_release_build { assert_eq!(out, format!("\ {} `rustc {dir}{sep}src{sep}lib.rs --crate-name test --crate-type lib \ --opt-level 3 \ + --cfg ndebug \ -C metadata=test:-:0.0.0:-:file:{dir} \ -C extra-filename={hash} \ --out-dir {dir}{sep}target{sep}release \ @@ -1075,6 +1076,7 @@ test!(verbose_release_build_deps { {running} `rustc {dir}{sep}foo{sep}src{sep}lib.rs --crate-name foo \ --crate-type lib \ --opt-level 3 \ + --cfg ndebug \ -C metadata=foo:-:0.0.0:-:file:{dir} \ -C extra-filename={hash1} \ --out-dir {dir}{sep}target{sep}release{sep}deps \ @@ -1082,6 +1084,7 @@ test!(verbose_release_build_deps { -L {dir}{sep}target{sep}release{sep}deps` {running} `rustc {dir}{sep}src{sep}lib.rs --crate-name test --crate-type lib \ --opt-level 3 \ + --cfg ndebug \ -C metadata=test:-:0.0.0:-:file:{dir} \ -C extra-filename={hash2} \ --out-dir {dir}{sep}target{sep}release \ @@ -1163,3 +1166,38 @@ test!(implicit_examples { assert_that(process(p.bin("test/hello")), execs().with_stdout("Hello, World!\n")); assert_that(process(p.bin("test/goodbye")), execs().with_stdout("Goodbye, World!\n")); }) + +test!(standard_build_no_ndebug { + let p = project("world") + .file("Cargo.toml", basic_bin_manifest("foo")) + .file("src/foo.rs", r#" + fn main() { + if cfg!(ndebug) { + println!("fast") + } else { + println!("slow") + } + } + "#); + + assert_that(p.cargo_process("cargo-build"), execs().with_status(0)); + assert_that(process(p.bin("foo")), execs().with_stdout("slow\n")); +}) + +test!(release_build_ndebug { + let p = project("world") + .file("Cargo.toml", basic_bin_manifest("foo")) + .file("src/foo.rs", r#" + fn main() { + if cfg!(ndebug) { + println!("fast") + } else { + println!("slow") + } + } + "#); + + assert_that(p.cargo_process("cargo-build").arg("--release"), + execs().with_status(0)); + assert_that(process(p.bin("release/foo")), execs().with_stdout("fast\n")); +}) -- 2.30.2